<?php
namespace App\Http\Controllers\Circle;

use App\Http\Controllers\Controller;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Image\ImagesDecorator;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Libraries\Decorator\User\UserPointDecorator;
use App\Libraries\Tools\Tools;
use App\Models\Circle\Circle;
use App\Models\Tool\BadWord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use OSS\Core\OssException;
use OSS\OssClient;


class CircleController extends Controller{


    private $circle;

    public function __construct(Circle $circle){
        $this->circle = $circle;
    }

    /**
     * 发朋友圈
     * @param Request $request
     * @param BadWord $badWord
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCircle(Request $request, BadWord $badWord){
        $validator = Validator::make($request->all(), [
            'content' => 'required|string|max:250',
            'images.*' => 'image',
        ], [
            'content.required' => '10001',
            'content.string' => '10002',
            'content.max' => '10002',
            'images.*.image' => '10003',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        if(!empty($request['content'])){
            $request['content'] = Tools::removeXSS($request['content']);
        }

        if($badWord->checkBadWord($request['content']) != 200){
            return $this->sendFailResponse(400, 40005);
        }

        $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
        $bucket = env('ALIYUN_BUCKET');
        $images = [];

        $this->circle->user_id = Auth::id();
        $this->circle->content = $request['content'];
        if(!empty($request['images'])){
            foreach($request->file('images') as $v){
                if($v->isValid()){
                    try{
                        $object = 'image/'.uniqid().'.'.$v->extension();
                        $ossClient->uploadFile($bucket, $object, $v->getFileInfo());
                    }catch (OssException $e){
                        return $this->sendFailResponse(500, 40003);
                    }
                    $images[] = $object;
                }
            }
        }
        if(!empty($images)){
            $this->circle->images = json_encode($images);
        }
        $this->circle->save();
        return $this->sendSuccessResponse([]);
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteCircle($id){
        $validator = Validator::make([
            'id' => $id,
        ], [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $circle = $this->circle->where('id', $id)->where('user_id', Auth::id())->first();
        if(!$circle){
            $this->sendFailResponse(400, 40101);
        }
        $circle->status = 0;
        $circle->save();
        return $this->sendSuccessResponse([]);
    }

    /**
     * 朋友圈
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function circles(Request $request){
        $validator = Validator::make($request->all(), [
            'user_id' => 'integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'user_id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->circle->addDecorator(new ImagesDecorator());
        $this->circle->addDecorator(new CoverDecorator());
        $this->circle->addDecorator(new IsEndDecorator());
        $this->circle->addDecorator(new UserPointDecorator());
        $data = $this->circle->circles($request['page'], $request['size'], $request['user_id']??0);
        return $this->sendSuccessResponse($data);
    }

}